from django.contrib.auth.models import User
from django.db import models

# Create your models here.
from utils.modelsMixin import ModelSetMixin, DateTimeModelMixin


class UserDetail(ModelSetMixin):
    GENDER_CHOICES = (
        (0, "女"),
        (1, "男")
    )
    avatar = models.TextField(null=True, blank=True, verbose_name="头像")
    sex = models.IntegerField(null=True, blank=True, verbose_name="性别", choices=GENDER_CHOICES)
    birthday = models.DateTimeField(null=True, blank=True, verbose_name="生日")
    phone = models.CharField(null=True, blank=True, verbose_name="手机号码", max_length=11, unique=True)
    intro = models.TextField(null=True, blank=True, verbose_name="自我介绍")
    user = models.OneToOneField(User, on_delete=models.CASCADE)


# 行政区划模型
class Area(models.Model):
    name = models.CharField(max_length=20, verbose_name="名称")
    parent = models.ForeignKey("self", on_delete=models.SET_NULL, null=True, blank=True, verbose_name="上级行政区划")

    class Meta:
        # 自定义表名
        db_table = "area"
        verbose_name = "行政区划"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 用户收货地址模型
class Address(DateTimeModelMixin):
    name = models.CharField(max_length=40, verbose_name="收货地址名")
    receiver = models.CharField(max_length=20, verbose_name="收货人")
    province = models.ForeignKey(Area, on_delete=models.PROTECT, verbose_name="省份", related_name="province_addresses")
    city = models.ForeignKey(Area, on_delete=models.PROTECT, verbose_name="城市", related_name="city_addresses")
    district = models.ForeignKey(Area, on_delete=models.PROTECT, verbose_name="区县", related_name="district_addresses")
    place = models.CharField(max_length=100, verbose_name="收货详细地址")
    mobile = models.CharField(max_length=11, verbose_name="手机号码")
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='address_set')

    class Meta:
        ordering = ["-update_time"]
        verbose_name = "收货地址"
        verbose_name_plural = verbose_name
